﻿using System;
using System.Data;
using System.Data.SqlClient;
using Microsoft.Practices.EnterpriseLibrary.Data.Sql;
using Microsoft.Practices.EnterpriseLibrary.Data;
using Csla;
using System.Collections;
using System.Collections.Generic;
using System.Reflection;

namespace Model
{
    [Serializable()]
    public class SoQuyTienMatThuQuyBO : Csla.BusinessBase<SoQuyTienMatThuQuyBO>
    {
        #region Business Properties

        private int? _ThuChiThuQuyID = -1;
        public int? ThuChiThuQuyID
        {
            get { return this._ThuChiThuQuyID; }
            set { this._ThuChiThuQuyID = value; }
        }

        private DateTime? _NgayLapPhieu = null;
        public DateTime? NgayLapPhieu
        {
            get { return this._NgayLapPhieu; }
            set { this._NgayLapPhieu = value; }
        }

        private string _So = null;
        public string So
        {
            get { return this._So; }
            set { this._So = value; }
        }

        private string _HoTen = null;
        public string HoTen
        {
            get { return this._HoTen; }
            set { this._HoTen = value; }
        }

        private string _DienGiai = null;
        public string DienGiai
        {
            get { return this._DienGiai; }
            set { this._DienGiai = value; }
        }

        private string _TaiKhoanDoiUng = null;
        public string TaiKhoanDoiUng
        {
            get { return this._TaiKhoanDoiUng; }
            set { this._TaiKhoanDoiUng = value; }
        }

        private double? _SoTienThu = null;
        public double? SoTienThu
        {
            get { return this._SoTienThu; }
            set
            {
                this._SoTienThu = value;
                if (value != null)
                    if (value == 0)
                        this._SoTienThu = null;
            }
        }

        private double? _SoTienChi = null;
        public double? SoTienChi
        {
            get { return this._SoTienChi; }
            set
            {
                this._SoTienChi = value;
                if (value != null)
                    if (value == 0)
                        this._SoTienChi = null;
            }
        }

        private double? _TonQuy = null;
        public double? TonQuy
        {
            get { return this._TonQuy; }
            set
            {
                this._TonQuy = value;
                if (value != null)
                    if (value == 0)
                        this._TonQuy = null;
            }
        }

        private int? _STT0 = 0;
        public int? STT0
        {
            get { return _STT0; }
            set { _STT0 = value; }
        }

        private int? _Flag = 0;
        public int? Flag
        {
            get { return _Flag; }
            set { _Flag = value; }
        }

        protected override object GetIdValue()
        {
            return _ThuChiThuQuyID.Value;
        }
        #endregion Business Properties
        //==========================================================================

        #region Factory Methods
        public void SetIsNew()
        {
            MarkNew();
        }

        public void SetIsOld()
        {
            MarkOld();
        }

        public void SetIsDirty()
        {
            MarkDirty();
        }

        public object GetValue(string fieldName)
        {
            PropertyInfo propInfo = this.GetType().GetProperty(fieldName);
            return propInfo.GetValue(this, null);
        }

        public void SetValue(string fieldName, object value)
        {
            PropertyInfo propInfo = this.GetType().GetProperty(fieldName);
            propInfo.SetValue(this, value, null);
            MarkDirty();
        }

        private static int newThuChiThuQuyID = -1;
        private SoQuyTienMatThuQuyBO()
        {
            _ThuChiThuQuyID = newThuChiThuQuyID--;
        }

        public SoQuyTienMatThuQuyBO(SqlDataReader dataReader)
        {
            MarkAsChild();
            Fetch(dataReader);
            MarkOld();
        }

        public SoQuyTienMatThuQuyBO(SqlDataReader dataReader, bool isRoot)
        {
            if (!isRoot)
                MarkAsChild();
            Fetch(dataReader);
            MarkOld();
        }

        public static SoQuyTienMatThuQuyBO NewRootSoQuyTienMatThuQuyBO()
        {
            return DataPortal.Create<SoQuyTienMatThuQuyBO>(new RootCriteria());
        }

        internal static SoQuyTienMatThuQuyBO NewChildSoQuyTienMatThuQuyBO()
        {
            return DataPortal.Create<SoQuyTienMatThuQuyBO>(new ChildCriteria());
        }

        public static SoQuyTienMatThuQuyBO GetRootSoQuyTienMatThuQuyBO(int _ThuChiThuQuyID)
        {
            return DataPortal.Fetch<SoQuyTienMatThuQuyBO>(new RootCriteria(_ThuChiThuQuyID));
        }

        public static SoQuyTienMatThuQuyBO GetRootSoQuyTienMatThuQuyBOByWhere(string where)
        {
            return DataPortal.Fetch<SoQuyTienMatThuQuyBO>(where);
        }

        internal static SoQuyTienMatThuQuyBO GetChildSoQuyTienMatThuQuyBO(SqlDataReader dataReader)
        {
            return new SoQuyTienMatThuQuyBO(dataReader);
        }

        public static void DeleteSoQuyTienMatThuQuyBO(int _ThuChiThuQuyID)
        {
            DataPortal.Delete(new RootCriteria(_ThuChiThuQuyID));
        }

        public static void DeleteSoQuyTienMatThuQuyBO(int _ThuChiThuQuyID, bool directDelete)
        {
            DataPortal.Delete(new RootCriteria(_ThuChiThuQuyID, directDelete));
        }
        #endregion Factory Methods
        //==========================================================================

        #region Criteria
        [Serializable()]
        public class RootCriteria
        {
            public int ThuChiThuQuyID;
            public bool directDelete = false;
            public RootCriteria(int _ThuChiThuQuyID)
            {
                this.ThuChiThuQuyID = _ThuChiThuQuyID;
                this.directDelete = false;
            }

            public RootCriteria(int _ThuChiThuQuyID, bool directDelete)
            {
                this.ThuChiThuQuyID = _ThuChiThuQuyID;
                this.directDelete = directDelete;
            }

            public RootCriteria() { }
        }

        [Serializable()]
        public class ChildCriteria
        {
            public int ThuChiThuQuyID;
            public bool directDelete = false;
            public ChildCriteria(int _ThuChiThuQuyID)
            {
                this.ThuChiThuQuyID = _ThuChiThuQuyID;
                this.directDelete = false;
            }

            public ChildCriteria(int _ThuChiThuQuyID, bool directDelete)
            {
                this.ThuChiThuQuyID = _ThuChiThuQuyID;
                this.directDelete = directDelete;
            }

            public ChildCriteria() { }
        }
        #endregion Criteria
        //==========================================================================

        #region Create
        [RunLocal]
        private void DataPortal_Create(RootCriteria criteria)
        {
        }

        private void DataPortal_Create(ChildCriteria criteria)
        {
            MarkAsChild();
            ValidationRules.CheckRules();
        }

        #endregion Create
        //==========================================================================

        #region Fetch
        [Transactional(TransactionalTypes.TransactionScope)]
        protected void DataPortal_Fetch(RootCriteria criteria)
        {
            this.Execute_Fetch(criteria);
        }

        protected void DataPortal_Fetch(string where)
        {
            this.Execute_Fetch(where);
        }

        protected void Execute_Fetch(RootCriteria criteria)
        {
            this.This_Fetch(criteria);
        }

        protected void Execute_Fetch(string where)
        {
            this.This_Fetch(where);
        }

        private void This_Fetch(RootCriteria criteria)
        {
            SqlDatabase db = new SqlDatabase(Session.DBConnectionString);
            SqlCommand cmd = (SqlCommand)db.GetStoredProcCommand("THThanhToanVoiNguoiMuaThuQuyBO_GetBO");
            db.AddInParameter(cmd, "@WhereExpression", SqlDbType.NVarChar, "[ThuChiThuQuyID] = " + criteria.ThuChiThuQuyID);
            using (SqlDataReader dataReader = (SqlDataReader)db.ExecuteReader(cmd))
            {
                if (dataReader.Read())
                    Fetch(dataReader);
            }
        }

        private void This_Fetch(string where)
        {
            SqlDatabase db = new SqlDatabase(Session.DBConnectionString);
            SqlCommand cmd = (SqlCommand)db.GetStoredProcCommand("THThanhToanVoiNguoiMuaThuQuyBO_GetBO");
            db.AddInParameter(cmd, "@WhereExpression", SqlDbType.NVarChar, where);
            using (SqlDataReader dataReader = (SqlDataReader)db.ExecuteReader(cmd))
            {
                if (dataReader.Read())
                    Fetch(dataReader);
            }
        }

        private void Fetch(SqlDataReader dataReader)
        {
            if (!(dataReader["NgayLapPhieu"] is DBNull))
                this._NgayLapPhieu = (DateTime)dataReader["NgayLapPhieu"];
            if (!(dataReader["So"] is DBNull))
                this._So = (string)dataReader["So"];
            if (!(dataReader["HoTen"] is DBNull))
                this._HoTen = (string)dataReader["HoTen"];
            if (!(dataReader["LyDo"] is DBNull))
                this._DienGiai = (string)dataReader["LyDo"];

            string No = "";
            string Co = "";
            if (!(dataReader["No"] is DBNull))
                No = (string)dataReader["No"];
            if (!(dataReader["Co"] is DBNull))
                Co = (string)dataReader["Co"];

            double SoTien = 0;
            if (!(dataReader["SoTien"] is DBNull))
                SoTien = (double)dataReader["SoTien"];

            if (No == "111")
            {
                this._TaiKhoanDoiUng = Co;
                this.SoTienThu = SoTien;
            }
            else
                if (Co == "111")
                {
                    this._TaiKhoanDoiUng = No;
                    this.SoTienChi = SoTien;
                }
        }
        #endregion Fetch
        //==========================================================================

        #region Insert
        [Transactional(TransactionalTypes.TransactionScope)]
        protected override void DataPortal_Insert()
        {
            this.Execute_Insert();
        }

        protected void Execute_Insert()
        {
            this.This_Insert();
        }

        private void This_Insert()
        {
        }
        #endregion Insert
        //==========================================================================

        #region Update
        [Transactional(TransactionalTypes.TransactionScope)]
        protected override void DataPortal_Update()
        {
            this.Execute_Update();
        }

        protected void Execute_Update()
        {
            this.This_Update();
        }

        private void This_Update()
        {
        }
        #endregion Update
        //==========================================================================

        #region Delete
        [Transactional(TransactionalTypes.TransactionScope)]
        protected void DataPortal_DeleteSelf()
        {
            DataPortal_Delete(new RootCriteria(_ThuChiThuQuyID.Value));
        }

        private void DataPortal_Delete(RootCriteria criteria)
        {
            this.Execute_Delete(criteria);
        }

        protected void Execute_Delete(RootCriteria criteria)
        {
        }
        #endregion Delete
        //==========================================================================

        #region Internal method
        internal void Insert()
        {
            if (!this.IsDirty) return;
            DataPortal_Insert();
            MarkOld();
        }

        internal void Update()
        {
            if (!this.IsDirty) return;
            DataPortal_Update();
            MarkOld();
        }

        internal void DeleteSelf()
        {
            if (!this.IsDirty) return;
            if (this.IsNew) return;
            DataPortal_Delete(new RootCriteria(_ThuChiThuQuyID.Value));
            MarkNew();
        }
        #endregion Internal method
        //==========================================================================
    }
}
